import { _decorator, Component, Node, sys, Button, EditBox } from 'cc';
import { GameManager } from './GameManager';
const { ccclass, property } = _decorator;

@ccclass('SettingPanel')
export class SettingPanel extends Component {
    @property(EditBox)
    totalTubeInput: EditBox = null; // 总试管数量输入框
    @property(EditBox)
    filledTubeInput: EditBox = null; // 需装满的试管数量输入框
    @property(EditBox)
    tubeCapacityInput: EditBox = null; // 每个试管的最大容量输入框

    @property(Button)
    downloadButton: Button = null; // 下载按钮
    @property(Button)
    reloadButton: Button = null; // 重载关卡按钮

    start() {
        this.totalTubeInput.string = '4'; // 设置默认总试管数量     
        this.filledTubeInput.string = '2'; // 设置默认需装满的试管数量
        this.tubeCapacityInput.string = '4'; // 设置默认每个试管的最大容量

        // 绑定下载按钮点击事件
        this.downloadButton.node.on(Button.EventType.CLICK, this.openDownloadUrl, this);
        // 绑定重载关卡按钮点击事件
        this.reloadButton.node.on(Button.EventType.CLICK, this.reloadLevel, this);
        // 绑定总试管数量输入框输入完成事件
        this.totalTubeInput.node.on(EditBox.EventType.EDITING_DID_ENDED, this.validateTotalTubes, this);
        // 绑定需装满的试管数量输入框输入完成事件
        this.filledTubeInput.node.on(EditBox.EventType.EDITING_DID_ENDED, this.validateFilledTubes, this);
        // 绑定试管容量输入框输入完成事件
        this.tubeCapacityInput.node.on(EditBox.EventType.EDITING_DID_ENDED, this.validateTubeCapacity, this);
    }

    // 打开下载链接方法
    openDownloadUrl() {
        sys.openURL('https://pan.baidu.com/s/1PFqGcW9h1d9ZHdp6Cucing?pwd=8sqy');
    }

    // 重载关卡方法
    reloadLevel() {
        const totalTubes = this.getValidTotalTubes();
        const filledTubes = this.getValidFilledTubes(totalTubes);
        const tubeCapacity = this.getValidTubeCapacity();

        GameManager.getInstance().reloadLevel(totalTubes, filledTubes, tubeCapacity);
    }

    // 获取有效的总试管数量
    getValidTotalTubes(): number {
        const inputValue = parseInt(this.totalTubeInput.string);
        if (isNaN(inputValue) || inputValue <= 3) {
            return 4;
        }
        return inputValue;
    }

    // 获取有效的需装满的试管数量
    getValidFilledTubes(totalTubes: number): number {
        const inputValue = parseInt(this.filledTubeInput.string);
        if (isNaN(inputValue) || inputValue <= 0 || inputValue >= totalTubes) {
            return Math.min(2, totalTubes - 1);
        }
        return inputValue;
    }

     // 获取有效的每个试管的最大容量
    getValidTubeCapacity(): number {
        const inputValue = parseInt(this.tubeCapacityInput.string);
        if (isNaN(inputValue) || inputValue < 1) {
            return 4;
        }
        return inputValue;
    }

    // 验证总试管数量输入值
    validateTotalTubes() {
        const validTotalTubes = this.getValidTotalTubes();
        if (validTotalTubes.toString() !== this.totalTubeInput.string) {
            this.totalTubeInput.string = validTotalTubes.toString();
            // 总试管数量变化后，重新验证需装满的试管数量
            this.validateFilledTubes();
        }
    }

    // 验证需装满的试管数量输入值
    validateFilledTubes() {
        const totalTubes = this.getValidTotalTubes();
        const validFilledTubes = this.getValidFilledTubes(totalTubes);
        if (validFilledTubes.toString() !== this.filledTubeInput.string) {
            this.filledTubeInput.string = validFilledTubes.toString();
        }
    }

    // 验证试管容量输入值
    validateTubeCapacity() {
        const validCapacity = this.getValidTubeCapacity();
        if (validCapacity.toString() !== this.tubeCapacityInput.string) {
            this.tubeCapacityInput.string = validCapacity.toString();
        }
    }
}